package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.dao.BaseDAO;
import com.emip.dao.ItemDAO;

@Repository("itemDAO")
public class ItemDAOImpl extends BaseDAO implements ItemDAO {

	public String add(ItemBean entity) {
		String id = (String)this.getHibernateTemplate().save(entity);
		return id;
	}

	public ItemBean findByID(String ID) {
		ItemBean bean = this.getHibernateTemplate().get(ItemBean.class, ID);		
		return bean;
	}

	public List<ItemBean> getAll() {
		List<ItemBean> result = this.getHibernateTemplate().find("from ItemBean m order by ID");
		return result;
	}

	public void remove(ItemBean entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(ItemBean entity) {
		this.getHibernateTemplate().update(entity);
	}

	public List<ItemBean> getItemsByMonitorType(MonitorTypeBean bean) {
		Object[] params = new Object[]{bean};
		List<ItemBean> result = this.getHibernateTemplate().find("from ItemBean where monitorType=? order by ID", params);
		return result;
	}

	public Boolean findExist(ItemBean entity) {
		Object[] params = new Object[] {entity.getId()};
		List<ItemBean> result = this.getHibernateTemplate().find("from ItemBean m where id=?", params);
		if (result.size()==0) {
			return false;
		}
		return true;
	}

	public ItemBean findByName(String name) {
		List<ItemBean> result = this.getHibernateTemplate().find("from ItemBean m where m.name = '" + name + "'");
		if (result != null && result.size() > 0) {
			return result.get(0);
		}
		return null;
	}

}
